﻿
// RightView.cpp : CRightView 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Aries.h"
#endif

#include "AriesDoc.h"
#include "MainFrm.h"
#include "RightView.h"
#include "LeftFormView.h"
#include "DBauthDlg.h"
#include <windows.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CRightView

IMPLEMENT_DYNCREATE(CRightView, CListView)

BEGIN_MESSAGE_MAP(CRightView, CListView)
	ON_WM_STYLECHANGED()
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_CREATE()
END_MESSAGE_MAP()

// CRightView 构造/析构

CRightView::CRightView()
{
	// TODO: 在此处添加构造代码

}

CRightView::~CRightView()
{
}

BOOL CRightView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CListView::PreCreateWindow(cs);
}

void CRightView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: 调用 GetListCtrl() 直接访问 ListView 的列表控件，
	//  从而可以用项填充 ListView。
}

void CRightView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CRightView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CRightView 诊断

#ifdef _DEBUG
void CRightView::AssertValid() const
{
	CListView::AssertValid();
}

void CRightView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CAriesDoc* CRightView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAriesDoc)));
	return (CAriesDoc*)m_pDocument;
}
#endif //_DEBUG


// CRightView 消息处理程序
void CRightView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
{
	//TODO: 添加代码以响应用户对窗口视图样式的更改	
	CListView::OnStyleChanged(nStyleType,lpStyleStruct);	
}


int CRightView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CListView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	GetDocument()->pRightView = this;

	return 0;
}


void CRightView::ResetView(void)
{
	CListCtrl &ctlLView = GetListCtrl();	
	ctlLView.DeleteAllItems();
	while(ctlLView.DeleteColumn(0))
		;
	UpdateWindow();
}


// 显示财报字段说明
void CRightView::DisplayField(void)
{
	CListCtrl &ctlField = this->GetListCtrl();	
	ResetView();
	GetListCtrl().ModifyStyle(NULL, LVS_REPORT);
	GetListCtrl().SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	ctlField.InsertColumn(0, _T("名称"), LVCFMT_LEFT, 400);
	ctlField.InsertColumn(1, _T("报表类型"), LVCFMT_LEFT, 200);
	ctlField.InsertColumn(2, _T("说明"), LVCFMT_LEFT, 640);
	

	int n;
	/* 设置ini文件所在目录为当前工作目录  */
	TCHAR path[255];   
    GetCurrentDirectory(255,path);
	TCHAR ininame[255];		
    GetModuleFileName(NULL, ininame, sizeof ininame); 
	TCHAR *ptr;
	ptr=_tcsrchr(ininame,'\\');
	if(ptr)   
		*(ptr+1)='\0'; 
    SetCurrentDirectory(ininame); 
    GetCurrentDirectory(255,path);

    /* 取ini文件内容 */
	n=GetPrivateProfileInt(_T("FinanceStatement"),_T("num"),0 ,_T(".//FinanceStatement.ini"));
	CString keyvalue;
	CString str;
	int row=0;

	for(int i=1;i<=n;i++)
	{
		str.Format(_T("%.2d"),i);
		CString keyname=_T("item")+str;
		GetPrivateProfileString(_T("FinanceStatement"),keyname,_T("No Found"),keyvalue.GetBuffer(60),60,_T(".//FinanceStatement.ini"));
		int curPos = 0;
		keyvalue.ReleaseBuffer();
		CString fsname=keyvalue.Tokenize(_T(","),curPos);
	    CString cnname=keyvalue.Tokenize(_T(","),curPos);
		
		n=GetPrivateProfileInt(fsname,_T("num"),0 ,_T(".//FinanceStatement.ini"));
		
		
		for(int j=1;j<=n;j++)
		{
			str.Format(_T("%.3d"),j);
			CString keyname=_T("item")+str;
			keyvalue=fsname;
			GetPrivateProfileString(fsname,keyname,_T("No Found"),keyvalue.GetBuffer(60),60,_T("./FinanceStatement.ini"));
			//int curPos = 0;
			//keyvalue.ReleaseBuffer();
			//CString resToken=keyvalue.Tokenize(_T(","),curPos);
			
			int nItem;
			nItem = ctlField.InsertItem(row, keyvalue);
			ctlField.SetItemText(nItem, 1, cnname);
			ctlField.SetItemText(nItem, 2, _T("现金或流动资金"));
	        row++;
	
		}
	}
}

// 显示财报内容
void CRightView::DisplayContent(int type)
{
	CListCtrl &ctlField = this->GetListCtrl();	
	ResetView();
	GetListCtrl().ModifyStyle(NULL, LVS_REPORT);
	GetListCtrl().SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	//判断DOC中对应的是哪张表的成员变量
	CAriesDoc *pDoc=(CAriesDoc *)GetDocument();	
	int nYear=_ttoi(pDoc->m_Year);
	//int nNyear=_ttoi(pDoc->m_Nyear);
	vector<vector <CString> > *ptrData;
	switch(type)
		{
			case 1:
				//sTbname=_T("stock.general.balancesheet");
				ptrData=&pDoc->m_BalanceSheet;
				break;
			case 2:
				//sTbname=_T("stock.general.profitstatement");
				ptrData=&pDoc->m_Profit;
				break;
			case 3:
				//sTbname=_T("stock.general.cashflow");
				ptrData=&pDoc->m_CashFlow;
				break;
		}

	//根据输入条件决定标题显示,从Doc类中调用成员变量
	int nNyear=(int) ptrData->size();   //因实际含有年数可能小于输入年数，在这里取实际值进行显示
	
	CString strYear;

	ctlField.InsertColumn(0, _T("条目"), LVCFMT_LEFT, 300);
	for(int i=1,j=nNyear;j>0;i++,j--,nYear--)
	{
		
		strYear.Format(_T("Y%d"),nYear);
		ctlField.InsertColumn(i, strYear, LVCFMT_LEFT, 190);
		/*ctlField.InsertColumn(2, _T("Y2012"), LVCFMT_LEFT, 190);
		ctlField.InsertColumn(3, _T("Y2011"), LVCFMT_LEFT, 190);
		ctlField.InsertColumn(4, _T("Y2010"), LVCFMT_LEFT, 190);
		ctlField.InsertColumn(5, _T("Y2009"), LVCFMT_LEFT, 190);*/		
	}

	int n;
	/* 设置ini文件所在目录为当前工作目录  */
	TCHAR path[255];   
    GetCurrentDirectory(255,path);
	TCHAR ininame[255];		
    GetModuleFileName(NULL, ininame, sizeof ininame); 
	TCHAR *ptr;
	ptr=_tcsrchr(ininame,'\\');
	if(ptr)   
		*(ptr+1)='\0'; 
    SetCurrentDirectory(ininame); 
    GetCurrentDirectory(255,path);

    /* 取ini文件内容 */
	n=GetPrivateProfileInt(_T("FinanceStatement"),_T("num"),0 ,_T(".//FinanceStatement.ini"));
	CString keyvalue;
	CString str;
	int row=0;

	
	str.Format(_T("%.2d"),type);
	CString keyname=_T("item")+str;
	GetPrivateProfileString(_T("FinanceStatement"),keyname,_T("No Found"),keyvalue.GetBuffer(60),60,_T(".//FinanceStatement.ini"));
	int curPos = 0;
	keyvalue.ReleaseBuffer();
	CString fsname=keyvalue.Tokenize(_T(","),curPos);
	CString cnname=keyvalue.Tokenize(_T(","),curPos);
		
	n=GetPrivateProfileInt(fsname,_T("num"),0 ,_T(".//FinanceStatement.ini"));

	for(int j=1;j<=n;j++)
	{
		str.Format(_T("%.3d"),j);
		CString keyname=_T("item")+str;
		keyvalue=fsname;
		GetPrivateProfileString(fsname,keyname,_T("No Found"),keyvalue.GetBuffer(60),60,_T("./FinanceStatement.ini"));
				
		int nItem;
		nItem = ctlField.InsertItem(row, keyvalue);	
		//nNyear=_ttoi(pDoc->m_Nyear);
		for (int i=0;i<nNyear;i++)
		{
				
			 //   char *str=PQgetvalue(res, i, j+2);     //命令结果按行从0开始,列从3开始
				//CString cs(str);							
				CString strRet=strValue((*ptrData)[i][j+2]);
				ctlField.SetItemText(nItem, i+1, strRet);					    
		}					
		row++;	
	}			
	//数据查询部分已移至DOC类处理，此处仅做VIEW的显示
	/*CString sqlstr,tbname;
	PGresult *res;
   	char * sqlcmd;
	int count=0;
	int iLen,iSize;
	CString sTbname;
	

	switch(type)
	{
		case 1:
			sTbname=_T("stock.general.balancesheet");
			break;
		case 2:
			sTbname=_T("stock.general.profitstatement");
			break;
		case 3:
			sTbname=_T("stock.general.cashflow");
		    break;
	}
    */
    
	//sqlstr.Format(_T("select * from %s WHERE \"SecuritiesCode\"='%s\' AND extract(month from \"DeclareDate\")=12 AND extract(year from \"DeclareDate\")<=%s ORDER BY \"DeclareDate\" DESC limit %s;"),sTbname,m_Code,m_Year,m_Nyear);
	//iLen=sqlstr.GetLength();
	//iSize = WideCharToMultiByte(CP_ACP, 0, sqlstr, -1, NULL, 0, NULL, NULL);
	//sqlcmd = (char*)malloc((iSize+1));
	//WideCharToMultiByte(CP_ACP, 0, sqlstr, -1, sqlcmd, iSize, NULL, NULL);	

	//res = PQexec(theApp.m_pConn, sqlcmd);


	//if (PQresultStatus(res) != PGRES_TUPLES_OK)    
	//	{
	//		AfxMessageBox(_T("SQL命令执行不成功！！！"));
	//		PQclear(res);
	//	}
	//else 
	//	{   
	//		for(int j=1;j<=n;j++)
	//		{
	//			str.Format(_T("%.3d"),j);
	//			CString keyname=_T("item")+str;
	//			keyvalue=fsname;
	//			GetPrivateProfileString(fsname,keyname,_T("No Found"),keyvalue.GetBuffer(60),60,_T("./FinanceStatement.ini"));
	//			
	//			int nItem;
	//			nItem = ctlField.InsertItem(row, keyvalue);
	//			nNyear=_ttoi(m_Nyear);
	//			for (int i=0;i<nNyear;i++)
	//			{
	//					char *str=PQgetvalue(res, i, j+2);     //命令结果按行从0开始,列从3开始
	//					CString cs(str);							
	//					CString strRet=strValue(cs);
	//					ctlField.SetItemText(nItem, i+1, strRet);					    
	//			}					
	//			row++;	
	//		}			
	//	
	//		PQclear(res);
	//	}
	//free(sqlcmd);		

}


// 格式转换，以千位分隔符隔开
CString CRightView::strValue(CString strData)
{
	CString strFormatLocale;
	TCHAR formatted[255];
	int ret = GetNumberFormat(GetUserDefaultLCID(),0,strData,NULL,formatted,255);
	strFormatLocale = formatted;
	return strFormatLocale;
}


void CRightView::DisplayFinancialAnalysis(int type)
{
	CListCtrl &ctlField = this->GetListCtrl();	
	ResetView();
	GetListCtrl().ModifyStyle(NULL, LVS_REPORT);
	GetListCtrl().SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	
	CAriesDoc *pDoc=(CAriesDoc *)GetDocument();	
	pDoc->FA_fcf();
	
	//根据输入条件决定标题显示
	int nYear=_ttoi(pDoc->m_Year);
	//int nNyear=_ttoi(pDoc->m_Nyear); 
	int nNyear=(int) pDoc->m_Fcf.size();   //因实际含有年数可能小于输入年数，在这里取实际值进行显示
	int i=1;
	CString strYear;

	ctlField.InsertColumn(0, _T("计算方法"), LVCFMT_LEFT, 300);
	for(int i=1,j=nNyear;j>0;i++,j--,nYear--)
	{
		
		strYear.Format(_T("Y%d"),nYear);
		ctlField.InsertColumn(i, strYear, LVCFMT_LEFT, 190);

	}
	int nItem;
	nItem = ctlField.InsertItem(1,_T("EBIT(1-T)=营业利润+财务费用-所得税费用"));	
	nItem = ctlField.InsertItem(2,_T("      加：资产减值准备"));
	nItem = ctlField.InsertItem(3,_T("      加：折旧"));
	nItem = ctlField.InsertItem(4,_T("      加：无形资产摊销"));
	nItem = ctlField.InsertItem(5,_T("      加：长期待摊费用摊销"));
	nItem = ctlField.InsertItem(6,_T("      加：固定资产报废损失"));
	nItem = ctlField.InsertItem(7,_T("∆营运成本"));
	nItem = ctlField.InsertItem(8,_T("      减：存货的减少"));
	nItem = ctlField.InsertItem(9,_T("      减：经营性应收项目的减少"));
	nItem = ctlField.InsertItem(10,_T("      减：经营性应付项目的增加"));
	nItem = ctlField.InsertItem(11,_T("      减：待摊费用的减少"));
	nItem = ctlField.InsertItem(12,_T("      减：预提费用的增加"));
	nItem = ctlField.InsertItem(13,_T("资本支出"));
	nItem = ctlField.InsertItem(14,_T("自由现金流量"));

	CString strRet;
	for (int i=0;i<nNyear;i++)
	{
				
		    strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].Noplat);
			ctlField.SetItemText(0, i+1, strValue(strRet));	
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].AssetsDepreciationReserves);
			ctlField.SetItemText(1, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].Depreciation);
			ctlField.SetItemText(2, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].IntangibleAssetAmortization);
			ctlField.SetItemText(3, i+1, strValue(strRet));	
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].DeferredExpenseAmortiztion);
			ctlField.SetItemText(4, i+1,strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].FixedAssetScrapLoss);
			ctlField.SetItemText(5, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].DeltaOperateCost);
			ctlField.SetItemText(6, i+1, strValue(strRet));
	        strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].InventoryDecrease);
			ctlField.SetItemText(7, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].OperateReceivableDecrease);
			ctlField.SetItemText(8, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].OperatePayableIncrease);
			ctlField.SetItemText(9, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].DeferredExpenseDecreased);
			ctlField.SetItemText(10, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].AccruedExpenseAdded);
			ctlField.SetItemText(11, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].CapitalPaid);
			ctlField.SetItemText(12, i+1, strValue(strRet));
			strRet.Format(_T("%.2f"),pDoc->m_Fcf[i].FCFvalue);
			ctlField.SetItemText(13, i+1, strValue(strRet));

	}	

	//for(int j=1;j<=n;j++)
	//{
	//	str.Format(_T("%.3d"),j);
	//	CString keyname=_T("item")+str;
	//	keyvalue=fsname;
	//	GetPrivateProfileString(fsname,keyname,_T("No Found"),keyvalue.GetBuffer(60),60,_T("./FinanceStatement.ini"));
	//			
	//	int nItem;
	//	nItem = ctlField.InsertItem(row, keyvalue);	
	//	//nNyear=_ttoi(pDoc->m_Nyear);
	//	for (int i=0;i<nNyear;i++)
	//	{
	//			
	//		 //   char *str=PQgetvalue(res, i, j+2);     //命令结果按行从0开始,列从3开始
	//			//CString cs(str);							
	//			CString strRet=strValue((*ptrData)[i][j+2]);
	//			ctlField.SetItemText(nItem, i+1, strRet);					    
	//	}					
	//	row++;	
	//}		
	
}
